﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 药品信息管理
{
    public partial class TBFP : Form
    {
        public string gysbm = "";//供应商编码
        string addtext = "";

        public TBFP()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textBox12_KeyPress(object sender, KeyPressEventArgs e)//供应商press
        {
            string cmdtext4 = "select bm 编码,MC AS 名称 from yk_zdgys where dh like '" + textBox12.Text.Trim() + "%' or bm like '" + textBox12.Text.Trim() + "%' or mc like '" + textBox12.Text.Trim() + "%'";
            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                cn.Open();
                SqlDataAdapter da1 = new SqlDataAdapter(cmdtext4, cn);
                DataTable dt = new DataTable();
                da1.Fill(dt);
                dataGridView2.DataSource = dt;
                dataGridView2.Visible = true;
                dataGridView2.Columns[0].Width = 60;
                dataGridView2.Columns[1].Width = 150;
            }

        }

        private void dataGridView2_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string cmdtext5 = "SELECT bm as 编码,MC AS 名称  from yk_zdgys where  dh = '" + dataGridView2.CurrentRow.Cells["编码"].Value.ToString().Trim() + "' or bm = '" + dataGridView2.CurrentRow.Cells["编码"].Value.ToString().Trim() + "' or mc = '" + dataGridView2.CurrentRow.Cells["编码"].Value.ToString().Trim() + "' ";
            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                cn.Open();
                SqlCommand com = new SqlCommand(cmdtext5, cn);
                SqlDataReader read = com.ExecuteReader();
                if (read.Read())
                {
                    textBox12.Text = read["名称"].ToString();
                    gysbm = read["编码"].ToString();
                    addtext = "  and gys.bm='" + gysbm + "'  ";
                }
                read.Close();
            }
            dataGridView2.Visible = false;
        }

        private void TBFP_Load(object sender, EventArgs e)
        {
            dataGridView2.Visible = false;
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.ReadOnly = true;
            this.dataGridView1.MultiSelect = false;
            this.dataGridView2.AllowUserToAddRows = false;
            this.dataGridView2.MultiSelect = false;
            this.dataGridView2.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView2.ReadOnly = true;
            fresh();
        }

        private void button3_Click(object sender, EventArgs e)//新单
        {
            int fpid = 0;
            try
            {

                string cmdtext1 = "select fpid from  yk_fp0  order by fpid desc ";
                SqlConnection cn = new SqlConnection(SQL.conStr);
                {
                    cn.Open();
                    SqlCommand com = new SqlCommand(cmdtext1, cn);
                    fpid = (int)com.ExecuteScalar();
                }
            }
            catch { MessageBox.Show("首次发票填补"); }
            Form f = new TBFPmx(fpid + 1);
            f.ShowDialog();
            fresh();
        }

        private void button4_Click(object sender, EventArgs e)//进入
        {
            Form f = new Form();
            try
            {
                f = new TBFPmx((int)dataGridView1.CurrentRow.Cells["发票ID"].Value);
            }
            catch { return; }
            f.ShowDialog();
            fresh();
        }

        private void button5_Click(object sender, EventArgs e)//删除
        {
            try
            {
                SqlConnection cn = new SqlConnection(SQL.conStr);
                cn.Open();
                string cmdtext3 = "   BEGIN TRANSACTION  " +
                                  "   Delete Yk_fp0 where fpid=" + dataGridView1.CurrentRow.Cells["发票ID"].Value +
                                  "   Delete Yk_fp1 where fpid=  " + dataGridView1.CurrentRow.Cells["发票ID"].Value +
                                  "   COMMIT TRANSACTION ";
                SqlCommand cmd = new SqlCommand(cmdtext3, cn);
                cmd.ExecuteNonQuery();
                fresh();
            }
            catch { return; } 
        
        }

        private void button2_Click(object sender, EventArgs e)//刷新
        {
            fresh();

        }

        public void fresh()//刷新
        {
            DataTable dt = new DataTable();
            string dtp1 = dateTimePicker1.Value.ToShortDateString();
            string dtp2 = dateTimePicker2.Value.ToShortDateString();
            SqlConnection cn = new SqlConnection(SQL.conStr);
            try
            {
                cn.Open();
                string cmdtext1 = "  select   distinct  F0.fpid as 发票ID,(select  convert(datetime,fprq-2)) as 发票日期,fph  as 发票号,gys.mc as  药品供应商   " +
                                "   FROM YK_FP0 F0  LEFT JOIN YK_ZDGYS gys ON F0.gysbm = gys.BM  " +
                                "   WHERE F0.YKBM='401'    " +addtext+
                                "   AND F0.fprq >=(select  convert(int,(select Convert(DateTime,'" + dtp1 + "')))+2 ) AND F0.fprq <=(select  convert(int,(select Convert(DateTime,'" + dtp2 + "')))+2 )  ORDER BY F0.fpid";

                SqlDataAdapter da = new SqlDataAdapter(cmdtext1, cn);
                da.Fill(dt);
                dataGridView1.DataSource = dt;
                dataGridView1.Columns[3].Width = 250;
            }
            catch { return; }
            addtext = "";
        }
    }
}